//
//  PEPlantTopoView.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/10/23.
//

#import "RHDeviceTopoHEBATView.h"
#define line_Width 1
@interface RHDeviceTopoHEBATView()
@property (nonatomic, strong) QMUILabel *solarTitleLab;
@property (nonatomic, strong) QMUILabel *gridTitleLab;
@property (nonatomic, strong) QMUILabel *generatorTitleLab;
@property (nonatomic, strong) QMUILabel *loadTitleLab;
//光伏功率
@property (nonatomic, strong) QMUILabel *solarLab;
//光伏电压
@property (nonatomic, strong) QMUILabel *solarVolLab;
//电网功率
@property (nonatomic, strong) QMUILabel *gridLab;
//电网电压
@property (nonatomic, strong) QMUILabel *gridVolLab;
//发电机功率
@property (nonatomic, strong) QMUILabel *generatorLab;
//发电机电压
@property (nonatomic, strong) QMUILabel *generatorVolLab;
//电池功率
@property (nonatomic, strong) QMUILabel *batteryPowerLab;
//电池电压
@property (nonatomic, strong) QMUILabel *batteryVolLab;
//负载功率
@property (nonatomic, strong) QMUILabel *loadLab;
//负载电压
@property (nonatomic, strong) QMUILabel *loadVolLab;
//逆变器功率
@property (nonatomic, strong) QMUILabel *inverterLab;
//逆变器电压
@property (nonatomic, strong) QMUILabel *inverterVolLab;

//soc
@property (nonatomic, strong) QMUILabel *socLab;
//soh
@property (nonatomic, strong) QMUILabel *sohLab;

@property (nonatomic, strong) UIImageView *solarImg;

@property (nonatomic, strong) UIImageView *inverterImg;

@property (nonatomic, strong) UIImageView *gridImg;

@property (nonatomic, strong) UIImageView *generatorImg;

@property (nonatomic, strong) UIImageView *batteryImg;

@property (nonatomic, strong) UIImageView *loadImg;

//光伏圆点
@property (nonatomic ,strong) UIView *solarDotView;
//电网圆点
@property (nonatomic ,strong) UIView *gridDotView;
//负载圆点
@property (nonatomic ,strong) UIView *loadDotView;
//电池圆点
@property (nonatomic ,strong) UIView *batteryDotView;
//发电机圆点
@property (nonatomic ,strong) UIView *generatorDotView;

//光伏动画
@property (nonatomic ,strong) UIBezierPath *solarPath;
//负载动画
@property (nonatomic ,strong) UIBezierPath *loadPath;
//电网
@property (nonatomic ,strong) UIBezierPath *gridPath;
//电池动画
@property (nonatomic ,strong) UIBezierPath *batteryPath;
//发电机动画
@property (nonatomic ,strong) UIBezierPath *generationPath;

@property (nonatomic ,assign) BOOL isLandscape;
@end
@implementation RHDeviceTopoHEBATView

-(instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        self.backgroundColor = [UIColor whiteColor];
     
        [self setupUI];
    }
    return self;
}

-(instancetype)initWithOrientation:(BOOL)isLandscape
{
    if(self = [super initWithFrame:CGRectZero]){
        self.backgroundColor = [UIColor whiteColor];
        self.isLandscape = isLandscape;
        [self setupUI];
    }
    return self;
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    [self setNeedsDisplay];
}


-(void)setupUI
{
    CGFloat valueFont = 10; 
    kWeakSelf;
    UIColor *titleColor = kCOLOR_HEX_A(@"#171A1D", 0.6); 
    //发电机
    UIImageView *generatorImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"kwt_home_topo_Generator"]];
    [self addSubview:generatorImg];
    self.generatorImg = generatorImg;
    [generatorImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(40);
        make.top.mas_offset(20);
        make.size.equalTo(@(CGSizeMake(32, 32)));
    }];
    
    QMUILabel *generatorTitleLab = [QMUILabel labelWithFont:kBOLDSYSTEMFONT(12) textColor:kCOLOR_HEX_A(@"#171A1D", 0.6)];
    generatorTitleLab.text = @"Generator";
    [self addSubview:generatorTitleLab];
    self.generatorTitleLab = generatorTitleLab;
    [generatorTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(generatorImg.mas_bottom).offset(5);
        make.centerX.equalTo(generatorImg);
    }];
    
    _generatorLab = [QMUILabel labelWithFont:kSYSTEMFONT(valueFont) textColor:kCOLOR_HEX(@"#3F4443")];
    _generatorLab.text = @"--kW";
    [self addSubview:_generatorLab];
    [_generatorLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(generatorImg.mas_right).offset(5);
        make.bottom.equalTo(generatorImg.mas_centerY).offset(-5);
    }];
    
    self.generatorLab.hidden = YES;
    
    //负载
    UIImageView *loadImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"kwt_home_topo_load"]];
    [self addSubview:loadImg];
    self.loadImg = loadImg;
    [loadImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(generatorImg);
        make.centerY.equalTo(generatorImg);
        make.right.mas_offset(-40);
    }];
    
    QMUILabel *loadTitleLab = [QMUILabel labelWithFont:kBOLDSYSTEMFONT(12) textColor:kCOLOR_HEX_A(@"#171A1D", 0.6)];
    loadTitleLab.text = @"Load";
    [self addSubview:loadTitleLab];
    self.loadTitleLab = loadTitleLab;
    [loadTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(loadImg.mas_bottom).offset(5);
        make.centerX.equalTo(loadImg);
    }];
    
    _loadLab = [QMUILabel labelWithFont:kSYSTEMFONT(valueFont) textColor:kCOLOR_HEX(@"#3F4443")];
    _loadLab.text = @"--kW";
    [self addSubview:_loadLab];
    [_loadLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(loadImg.mas_centerY).offset(-5);
        make.right.equalTo(loadImg.mas_left).offset(-5);
    }];
    
    
    //光伏
    UIImageView *solarImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"kwt_home_topo_solar"]];
    [self addSubview:solarImg];
    self.solarImg = solarImg;
    [solarImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(generatorImg);
        make.left.equalTo(generatorImg);
        make.top.equalTo(weakSelf.generatorTitleLab.mas_bottom).offset(weakSelf.isLandscape?60:15);
    }];
    
    QMUILabel *solarTitleLab = [QMUILabel labelWithFont:kBOLDSYSTEMFONT(12) textColor:kCOLOR_HEX_A(@"#171A1D", 0.6)];
    solarTitleLab.text = @"Solar";
    self.solarTitleLab = solarTitleLab;
    [self addSubview:solarTitleLab];
    [solarTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(solarImg.mas_bottom).offset(5);
        make.centerX.equalTo(solarImg);
    }];
    
    _solarLab = [QMUILabel labelWithFont:kSYSTEMFONT(valueFont) textColor:kCOLOR_HEX(@"#3F4443")];
    _solarLab.text = @"--kW";
    [self addSubview:_solarLab];
    [_solarLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(solarTitleLab.mas_bottom).offset(5);
        make.centerX.equalTo(solarTitleLab).offset(-0);
    }];
    
    
    //逆变器
    UIImageView *inverterImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"kwt_home_topo_inverter"]];
    [self addSubview:inverterImg];
    self.inverterImg = inverterImg;
    [inverterImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(solarImg);
        make.centerX.mas_offset(0);
        make.centerY.equalTo(solarImg);
    }];
    
    QMUILabel *inverterTitleLab = [QMUILabel labelWithFont:kBOLDSYSTEMFONT(12) textColor:kCOLOR_HEX_A(@"#171A1D", 0.6)];
    inverterTitleLab.text = @"Inverter";
    [self addSubview:inverterTitleLab];
    [inverterTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(weakSelf.inverterImg.mas_bottom).mas_offset(5);
        make.right.equalTo(weakSelf.inverterImg.mas_left).offset(5);
    }];
    
    _inverterLab = [QMUILabel labelWithFont:kSYSTEMFONT(valueFont) textColor:kCOLOR_HEX(@"#3F4443")];
    _inverterLab.text = @"--kW";
    [self addSubview:_inverterLab];
    [_inverterLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(inverterTitleLab.mas_top).offset(-5);
        make.centerX.equalTo(inverterTitleLab);
    }];
    
    self.inverterLab.hidden = YES;
    
    //电网
    UIImageView *gridImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"kwt_home_topo_Grid"]];
    [self addSubview:gridImg];
    self.gridImg = gridImg;
    [gridImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(solarImg);
        make.centerY.equalTo(solarImg);
        make.right.equalTo(loadImg);
    }];
    
    QMUILabel *gridTitleLab = [QMUILabel labelWithFont:kBOLDSYSTEMFONT(12) textColor:kCOLOR_HEX_A(@"#171A1D", 0.6)];
    gridTitleLab.text = @"Grid";
    [self addSubview:gridTitleLab];
    self.gridTitleLab = gridTitleLab;
    [gridTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(gridImg.mas_bottom).offset(5);
        make.centerX.equalTo(gridImg);
    }];
    
    _gridLab = [QMUILabel labelWithFont:kSYSTEMFONT(valueFont) textColor:kCOLOR_HEX(@"#3F4443")];
    _gridLab.text = @"--kW";
    [self addSubview:_gridLab];
    [_gridLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(gridTitleLab.mas_bottom).offset(5);
        make.centerX.equalTo(gridTitleLab);
    }];
    

    
    //电池
    UIImageView *batteryImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"kwt_home_topo_battery"]];
    [self addSubview:batteryImg];
    self.batteryImg = batteryImg;
    [batteryImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@(CGSizeMake(27, 41)));
        make.centerX.equalTo(inverterImg);
        make.bottom.mas_offset(-15);
    }];
    
    QMUILabel *batteryTitleLab = [QMUILabel labelWithFont:kSYSTEMFONT(10) textColor:titleColor textAligment:NSTextAlignmentLeft];
    batteryTitleLab.text = @"Power";
    [self addSubview:batteryTitleLab];
    [batteryTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(batteryImg).offset(5);
        make.left.equalTo(batteryImg.mas_right).offset(20);
    }];
    
    _batteryPowerLab = [QMUILabel labelWithFont:kSYSTEMFONT(12) textColor:kCOLOR_HEX(@"#3F4443") textAligment:NSTextAlignmentLeft];
    _batteryPowerLab.text = @"--W";
    [self addSubview:_batteryPowerLab];
    [_batteryPowerLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(batteryTitleLab.mas_bottom).offset(5);
        make.left.equalTo(batteryTitleLab);
    }];

    
    
    QMUILabel *socTitleLab = [QMUILabel labelWithFont:kSYSTEMFONT(10) textColor:titleColor textAligment:NSTextAlignmentLeft];
    socTitleLab.text = @"SOC";
    [self addSubview:socTitleLab];
    [socTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(batteryImg).offset(5);
        make.right.equalTo(batteryImg.mas_left).offset(- 5);
        make.width.offset(40);
    }];
    
    _socLab = [QMUILabel labelWithFont:kSYSTEMFONT(12) textColor:kCOLOR_HEX(@"#3F4443") textAligment:NSTextAlignmentLeft];
    _socLab.adjustsFontSizeToFitWidth = YES;
    _socLab.minimumScaleFactor = 0.8;
    _socLab.text = @"--%";
    [self addSubview:_socLab];
    [_socLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(socTitleLab.mas_bottom).offset(5);
        make.left.equalTo(socTitleLab);
        make.width.equalTo(socTitleLab);
    }];
    
    
    
    
    //------ 小圆点
    _solarDotView = [[UIView alloc] initWithFrame:CGRectMake(-200, 0, 6, 6)];
    [_solarDotView setBackgroundColor:kCOLOR_HEX_A(@"#F9C626", 1)];
    [_solarDotView.layer setCornerRadius:3];
    [self addSubview:_solarDotView];
    
    _gridDotView = [[UIView alloc] initWithFrame:CGRectMake(-200, 0, 6, 6)];
    [_gridDotView setBackgroundColor:kCOLOR_HEX_A(@"#42D5EA", 1)];
    [_gridDotView.layer setCornerRadius:3];
    [self addSubview:_gridDotView];
    
    _loadDotView = [[UIView alloc] initWithFrame:CGRectMake(-200, 0, 6, 6)];
    [_loadDotView setBackgroundColor:kCOLOR_HEX_A(@"#F9A4A3", 1)];
    [_loadDotView.layer setCornerRadius:3];
    [self addSubview:_loadDotView];
    
    _batteryDotView = [[UIView alloc] initWithFrame:CGRectMake(-200, 0, 6, 6)];
    [_batteryDotView setBackgroundColor:kCOLOR_HEX_A(@"#76D16E", 1)];
    [_batteryDotView.layer setCornerRadius:3];
    [self addSubview:_batteryDotView];

    _generatorDotView = [[UIView alloc] initWithFrame:CGRectMake(-200, 0, 6, 6)];
    [_generatorDotView setBackgroundColor:kCOLOR_HEX_A(@"#678EF2", 1)];
    [_generatorDotView.layer setCornerRadius:3];
    [self addSubview:_generatorDotView];

    
    self.solarLab.hidden = self.deviceModel.productTypeId == ProductType_HE_BAT;
   
    
    self.generatorLab.hidden = self.deviceModel.productTypeId == ProductType_HE_BAT;
    
    
    self.gridLab.hidden = self.deviceModel.productTypeId == ProductType_HE_BAT;
    
    
    self.loadLab.hidden = self.deviceModel.productTypeId == ProductType_HE_BAT;
    
}



- (void)drawRect:(CGRect)rect
{
    //-------- 连线
    UIBezierPath *solar_inver = [UIBezierPath bezierPath];
    solar_inver.lineWidth = line_Width;
   
    [kCOLOR_HEX(@"#F9C626") setStroke];
    [solar_inver moveToPoint:CGPointMake(self.solarImg.right, self.solarImg.centerY)];
    [solar_inver addLineToPoint:CGPointMake(self.inverterImg.left , self.solarImg.centerY)];
    [solar_inver stroke];
    self.solarPath = solar_inver;
 
    //-------- 连线
    UIBezierPath *inverter_grid = [UIBezierPath bezierPath];
    inverter_grid.lineWidth = line_Width;
   
    [kCOLOR_HEX(@"#5AD8A6")  setStroke];
    [inverter_grid moveToPoint:CGPointMake(self.inverterImg.right, self.inverterImg.centerY)];
    [inverter_grid addLineToPoint:CGPointMake(self.gridImg.left , self.gridImg.centerY)];
    [inverter_grid stroke];
    self.gridPath = inverter_grid;
    
    //-------- 连线
    UIBezierPath *generator_inverter = [UIBezierPath bezierPath];
    generator_inverter.lineWidth = line_Width;
    [kCOLOR_HEX(@"#678EF2") setStroke];
    [generator_inverter moveToPoint:CGPointMake(self.generatorImg.right, self.generatorImg.centerY)];
    [generator_inverter addLineToPoint:CGPointMake(self.inverterImg.centerX-5-5 , self.generatorImg.centerY)];
    UIBezierPath *circle_generator = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.inverterImg.centerX-5-5, self.generatorImg.centerY+5) radius:5 startAngle:M_PI+M_PI_2 endAngle:M_PI*2 clockwise:YES];
    [generator_inverter appendPath:circle_generator];
    [generator_inverter addLineToPoint:CGPointMake(self.inverterImg.centerX-5 , self.inverterImg.top)];
    [generator_inverter stroke];
    self.generationPath = generator_inverter;
    
    //-------- 连线
    UIBezierPath *inverter_battery = [UIBezierPath bezierPath];
    inverter_battery.lineWidth = line_Width;
    [kCOLOR_HEX(@"#5AD8A6") setStroke];
    [inverter_battery moveToPoint:CGPointMake(self.inverterImg.centerX, self.inverterImg.bottom )];
    [inverter_battery addLineToPoint:CGPointMake(self.batteryImg.centerX, self.batteryImg.top)];
    [inverter_battery stroke];
    self.batteryPath = inverter_battery;
    
    //-------- 连线
    UIBezierPath *inverter_load = [UIBezierPath bezierPath];
    inverter_load.lineWidth = line_Width;
    [kCOLOR_HEX(@"#F9A4A3") setStroke];
    [inverter_load moveToPoint:CGPointMake(self.loadImg.left ,self.loadImg.centerY)];
    [inverter_load addLineToPoint:CGPointMake(self.inverterImg.centerX+5+5 ,self.loadImg.centerY)];
    UIBezierPath *circle_load = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.inverterImg.centerX+5+5, self.loadImg.centerY+5) radius:5 startAngle:M_PI+M_PI_2 endAngle:M_PI clockwise:NO];
    [inverter_load appendPath:circle_load];
    [inverter_load addLineToPoint:CGPointMake(self.inverterImg.centerX+5 ,self.inverterImg.top)];
    [inverter_load stroke];
    self.loadPath = [inverter_load bezierPathByReversingPath];
}



//设备进来  //只有电池才除以1000
-(void)setTopoModel:(RHTopoModel *)topoModel deviceModel:(nonnull RHDeviceModel *)deviceModel
{
    self.topoModel = topoModel;
    self.deviceModel = deviceModel;
    
    BOOL isHeBat = self.deviceModel.productTypeId == ProductType_HE_BAT;
    BOOL isMe_61 = [self.deviceModel.deviceTypeCode is61DeviceType];//工商储61度电没有发电机
    //---------- HE_BAT  置灰
    self.solarLab.hidden = isHeBat;
    self.generatorLab.hidden = isHeBat;
    self.gridLab.hidden = isHeBat;
    self.loadLab.hidden = isHeBat;
    

    NSString *solarImageStr = isHeBat?@"kwt_home_topo_solar_gray":@"kwt_home_topo_solar";
    self.solarImg.image = [UIImage imageNamed:solarImageStr];
    
    NSString *gridImageStr = isHeBat?@"kwt_home_topo_Grid_gray":@"kwt_home_topo_Grid";
    self.gridImg.image = [UIImage imageNamed:gridImageStr];
    
    NSString *generatorImageStr = (isHeBat||isMe_61)?@"kwt_home_topo_Generator_gray":@"kwt_home_topo_Generator";
    self.generatorImg.image = [UIImage imageNamed:generatorImageStr];
    
    NSString *loadImageStr = isHeBat?@"kwt_home_topo_load_gray":@"kwt_home_topo_load";
    self.loadImg.image = [UIImage imageNamed:loadImageStr];
    
    //设备在线并且逆变器离线 图标置灰
    BOOL invState = (self.deviceModel.invState == 0&&[self.deviceModel.status isEqualToString:@"0"]);
    
    NSString *InvImageStr = invState?@"kwt_home_topo_Inv_gray":@"kwt_home_topo_inverter";
    self.inverterImg.image = [UIImage imageNamed:InvImageStr];
    
    //纳通高压没有发电机,置灰
    if ([self.deviceModel.deviceTypeCode isHVDeviceType]) {
        self.generatorLab.hidden = YES;
        self.generatorVolLab.hidden = YES;
        self.generatorTitleLab.textColor = kCOLOR_HEX(@"#969799");
        self.generatorImg.image = [UIImage imageNamed:@"kwt_home_topo_Generator_gray"];
    }
    //-------------------------------------------------------------------------
    
    self.solarLab.text        = @"--kW";
    self.gridLab.text         = @"--kW";
    self.loadLab.text         = @"--kW";
    self.generatorLab.text    = @"--kW";
    self.batteryPowerLab.text = @"--kW";

    self.socLab.text = @"--%";
    self.sohLab.text = @"--%";
    
    self.solarDotView.hidden = YES;
    self.gridDotView.hidden = YES;
    self.loadDotView.hidden = YES;
    self.batteryDotView.hidden = YES;
    self.generatorDotView.hidden = YES;
    
    //设备离线
    if(![self.deviceModel.status isEqualToString:@"0"]) return;
   
    
    //电池功率
    NSDecimalNumber *batteryPower = [NSDecimalNumber decimalNumberWithDouble:[topoModel.batPwr doubleValue]];
    self.batteryPowerLab.text = [NSString stringWithFormat:@"%@kW",batteryPower];
    
    //soc
    NSString *socStr = [NSString stringWithFormat:@"%@%%",[NSDecimalNumber decimalPlacesWithRoundUpWithDigit:3 value:topoModel.soc]];
    self.socLab.text = [NSString stringWithFormat:@"%@",socStr];
    
    
    //逆变器离线只显示电池数据 和动画
    if (self.deviceModel.invState == 0) {
        //电池动画
        if([topoModel.batPwr floatValue]>0){
            self.batteryDotView.hidden = NO;
            [self addAnimationWithPointView:self.batteryDotView path:self.batteryPath duration:3 animationName:@"battery"];
        }else if ([topoModel.batPwr floatValue] < 0){
            self.batteryDotView.hidden = NO;
            [self addAnimationWithPointView:self.batteryDotView path:self.batteryPath.bezierPathByReversingPath duration:3 animationName:@"battery_rever"];
        }else{
            [self.batteryDotView.layer removeAllAnimations];
        }
        return;
    }
    
    //光伏功率
    self.solarLab.text = [NSString stringWithFormat:@"%@kW",[NSDecimalNumber decimalPlacesWithRoundUpWithDigit:3 value:[topoModel.pvPwr doubleValue]]];
    
    //电网功率
    self.gridLab.text = [NSString stringWithFormat:@"%@kW",[NSDecimalNumber decimalPlacesWithRoundUpWithDigit:3 value:[topoModel.gridPwr doubleValue]]];
    
    //负载功率
    self.loadLab.text = [NSString stringWithFormat:@"%@kW",[NSDecimalNumber decimalPlacesWithRoundUpWithDigit:3 value:[topoModel.loadPwr doubleValue]]];
    
 
    
    //发电机功率
    self.generatorLab.text = [NSString stringWithFormat:@"%@kW",[NSDecimalNumber decimalPlacesWithRoundUpWithDigit:3 value:[topoModel.genPwr doubleValue]]];
    
    
    //电池动画
    if([topoModel.batPwr floatValue]>0){
        self.batteryDotView.hidden = NO;
        [self addAnimationWithPointView:self.batteryDotView path:self.batteryPath duration:3 animationName:@"battery"];
    }else if ([topoModel.batPwr floatValue] < 0){
        self.batteryDotView.hidden = NO;
        [self addAnimationWithPointView:self.batteryDotView path:self.batteryPath.bezierPathByReversingPath duration:3 animationName:@"battery_rever"];
    }else{
        [self.batteryDotView.layer removeAllAnimations];
    }
    
    //光伏动画
    if([topoModel.pvPwr floatValue]>0){
        self.solarDotView.hidden = NO;
        [self addAnimationWithPointView:self.solarDotView path:self.solarPath duration:3 animationName:@"solar"];
    }else{
        [self.solarDotView.layer removeAllAnimations];
    }
    
   //电网动画  正:grid---->inverter    负:inverter -----> grid
    if([topoModel.gridPwr floatValue]<0){
        self.gridDotView.hidden = NO;
        [self addAnimationWithPointView:self.gridDotView path:self.gridPath duration:3 animationName:@"grid"];
    }else if ([topoModel.gridPwr floatValue]> 0){
        self.gridDotView.hidden = NO;
        [self addAnimationWithPointView:self.gridDotView path:self.gridPath.bezierPathByReversingPath duration:3 animationName:@"grid_rever"];
    }else{
        [self.gridDotView.layer removeAllAnimations];
    }
    
    //负载动画
    if([topoModel.loadPwr floatValue]>0){
        self.loadDotView.hidden = NO;
        [self addAnimationWithPointView:self.loadDotView path:self.loadPath duration:3 animationName:@"load"];
    }else{
        [self.loadDotView.layer removeAllAnimations];
    }
    
    //发电机动画
    if([topoModel.genPwr floatValue]>0){
        self.generatorDotView.hidden = NO;
        [self addAnimationWithPointView:self.generatorDotView path:self.generationPath duration:3 animationName:@"genrator"];
    }else{
        [self.generatorDotView.layer removeAllAnimations];
    }
}

-(void)addAnimationWithPointView:(UIView*)pointView path:(UIBezierPath*)path duration:(NSTimeInterval)duration animationName:(NSString*)name
{
    [pointView.layer removeAllAnimations];
    CAKeyframeAnimation *pointAnimation = [self createAnimationWithPath:path duration:duration];
    [pointView.layer addAnimation:pointAnimation forKey:name];
}

-(CAKeyframeAnimation*)createAnimationWithPath:(UIBezierPath*)path duration:(NSTimeInterval)duration
{
    CAKeyframeAnimation *keyFrameAni = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    keyFrameAni.path = path.CGPath;
    keyFrameAni.removedOnCompletion = NO;
    keyFrameAni.duration = duration;
    keyFrameAni.repeatCount = MAXFLOAT;
    keyFrameAni.calculationMode = kCAAnimationPaced;;
    
    return keyFrameAni;
}

@end
